package youleche.com.business;

import java.sql.Connection;
import java.util.List;

import youleche.com.connection.ConnectionFactory;
import youleche.com.factory.DaoFactory;
import youleche.com.idao.IArea;
import youleche.com.model.MArea;

public class BArea {
	/**
	 * 数据库连接
	 */
	private Connection con = null;
	
	/**
	 * 处理器
	 */
	private IArea handler = null;
	
	/**
	 * 构造器
	 */
	public BArea(){
		this.handler = DaoFactory.getDArea();
	}
	
	/**
	 * 添加行政区域数据
	 * @param area 区域数据
	 * @return 返回影响的行数
	 * @throws Exception 异常
	 */
	public int addArea(MArea area) throws Exception{
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.handler.addArea(this.con, area);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 通过代码判断是否存在区域数据
	 * @param code 代码区域
	 * @return 返回是否存在 true 存在
	 * @throws Exception 异常
	 */
	public boolean isExistAreaByCode(String code)
			throws Exception{
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.isExistAreaByCode(this.con, code);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 根据父亲code得到区域数据
	 * @param parentCode 父亲code
	 * @return 返回数据集合
	 * @throws Exception 异常
	 */
	public List<MArea> getAreaByParentCode(String parentCode)
			throws Exception{
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getAreaByParentCode(this.con, parentCode);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 根据城市和等级获取区域数据(直辖市)
	 * @param con 数据库连接
	 * @param areaName 区域名
	 * @return 直辖市区域数据
	 * @throws Exception
	 */
	public List<MArea> getAreaByArealevelAndAreaNameForZXS(String areaName) throws Exception{
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getAreaByArealevelAndAreaNameForZXS(this.con, areaName);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 根据城市和等级获取区域数据
	 * @param con 数据库连接
	 * @param areaName 区域名
	 * @return 区域数据
	 * @throws Exception
	 */
	public List<MArea> getAreaByArealevelAndAreaName(String areaName) throws Exception{
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getAreaByArealevelAndAreaNameForZXS(this.con, areaName);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 根据区域名称得到区域
	 * @param name 区域名称
	 * @return 返回区域数据
	 * @throws Exception 异常
	 */
	public MArea getAreaByAreaName(String name)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getAreaByAreaName(this.con, name);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
/**
	 * 获取中国行政区域数据最后一条记录（最新一条）code
	 * @return 获取最后一条code
	 * @throws Exception 异常
	 */
	public String getOnlyOneAreaByAddTimeDesc() throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getOnlyOneAreaByAddTimeDesc(this.con);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 根据code得到区域数据
	 * @param code code
	 * @return 返回数据集合
	 * @throws Exception 异常
	 */
	public MArea getAreaByAreaCode(String code)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getAreaByAreaCode(this.con, code);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
}
